Apply Set Operations On Redis Sets Using Python And Redis-Py

Overview:

Redis supports several mathematical operations that are defined on sets, which include

  • Membership of an element in a set
  • Union of sets
  • Intersection sets
  • Subtraction of sets

Set Membership:

  • Any Set consists of one or more elements. A set without any member is called a null set.
  • The Redis command, SISMEMBER checks whether an element is a member of a set.

 

Example:

# Example Python program to demonstrate set membership operation in Redis

import redis

 

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

                               

# Define a set of prime numbers

redisClient.sadd("primes", 2, 3, 5, 7)

                               

# Check if 6 is a prime number

print("Is 6 a prime number? {}".format(redisClient.sismember("primes", 8)))

                               

# Check if 7 is a prime number

print("Is 7 a prime number? {}".format(redisClient.sismember("primes", 7)))

 

 

Output:

Is 6 a prime number? False

Is 7 a prime number? True

Set Union:

  • Union of 'n' sets is a set, which has elements that are part of either the individual sets or part of many of them.
  • The Redis command SUNION provides the Union of multiple sets.
  • The command SUNIONSTORE provided by Redis is similar to SUNION. However, SUNIONSTORE stores the resultant union of multiple sets in a destination set provided.

  Example:

# Example Python program to demonstrate set union in Redis

import redis

 

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

 

# Define multiple sets of fruits

redisClient.sadd("Fruits1", "Pear",  "Peach", "Guava", "Melon")                               

redisClient.sadd("Fruits2", "Apple", "Melon", "Berry", "Mango")                               

redisClient.sadd("Fruits3", "Grapes", "Banana", "Melon", "Berry") 

 

# Find the union of the redis sets

print(redisClient.sunion("Fruits1","Fruits2","Fruits3"))

 

# Define multiple sets of games

redisClient.sadd("Games1", "Cricket", "Hockey", "Base Ball")                                

redisClient.sadd("Games2", "Foot Ball", "Badminton", "Hockey")                               

redisClient.sadd("Games3", "Tennis", "Hockey", "Polo") 

 

# Find the union of the redis sets and store it in the destination set provided

redisClient.sunionstore("Games4", "Games1","Games2","Games3")

# Print the members of the new set

print(redisClient.smembers("Games4"))

 

Output:

{b'Guava', b'Grapes', b'Pear', b'Peach', b'Apple', b'Melon', b'Berry', b'Banana', b'Mango'}

{b'Polo', b'Badminton', b'Base Ball', b'Hockey', b'Foot Ball', b'Tennis', b'Cricket'}

 

Set Intersection:

  • Intersection of multiple sets is provided by a set whose elements are present in all of those sets for which the Intersection is taken.
  • Redis has two commands to perform set intersection. One is SINTER and the other one is SINTERSTORE.
  • SINTER computes intersection of multiple sets and returns the resultant set.
  • SINTERSTORE is similar to SINTER. Instead of returning the intersection of multiple sets, SINTERSTORE stores it in the destination set provided.

 

Example:

# Example Python program to demonstrate set intersection in Redis

import redis

 

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

 

# Define multiple sets of fruits in Redis

redisClient.sadd("FruitBasket1", "Pear", "Peach", "Guava", "Melon")                               

redisClient.sadd("FruitBasket2", "Apple", "Melon", "Berry", "Mango")                               

redisClient.sadd("FruitBasket3", "Grapes", "Banana", "Melon", "Berry")                               

 

# Find the intersection of multiple sets of fruits

newBasket = redisClient.sinter("FruitBasket1", "FruitBasket2", "FruitBasket3")

print("Intersection of fruit baskets:")

print(newBasket)

 

# Define multiple sets of games in Redis

redisClient.sadd("GameSet1", "Cricket", "Hockey", "Base Ball")                                

redisClient.sadd("GameSet2", "Foot Ball", "Badminton", "Hockey")                               

redisClient.sadd("GameSet3", "Tennis", "Hockey", "Polo")                               

 

# Find the intersection of multiple sets of games

redisClient.sinterstore("GameSetCommon","GameSet1", "GameSet2", "GameSet3")

print("Intersection of game sets:")

print(redisClient.smembers("GameSetCommon"))

 

Output:

Intersection of fruit baskets:

{b'Melon'}

Intersection of game sets:

{b'Hockey'}

Subtraction of sets:

  • Removing elements of A, which are also present in the other sets, provides the subtraction of multiple sets from a set A.
  • Redis provides SDIFF that computes and returns the difference set between the set A and all other sets.
  • The SDIFFSTORE command computes the difference set between the set A and all other sets. The difference set is stored in a destination set provided.

Example:

# Example Python program to demonstrate set difference operation in Redis

import redis

 

# Create a Redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

 

# Define multiple sets of numbers

redisClient.sadd("NumberSet1", 1, 2, 3, 4)

redisClient.sadd("NumberSet2", 2)                      

redisClient.sadd("NumberSet3", 3)

 

# Find the set difference between set 1 and all other sets

print(redisClient.sdiff("NumberSet1", "NumberSet2", "NumberSet3"))

 

# Define multiple sets of credit ratings

redisClient.sadd("CreditRatings1", "AAA", "AA", "BB")                               

redisClient.sadd("CreditRatings2", "BB")                               

redisClient.sadd("CreditRatings3", "AA") 

 

# Find the set difference between CreditRatings set 1 and all other sets

# Store the results in CreditRatings4

redisClient.sdiffstore("CreditRatings4", "CreditRatings1", "CreditRatings2", "CreditRatings3")

print(redisClient.smembers("CreditRatings4"))

 

Output:

{b'4', b'1'}

{b'AAA'}

 


Copyright 2023 © pythontic.com